//
//  CAFollowListCell.m
//  cascade-realm
//
//  Created by QSC on 3/6/24.
//

#import "CAFollowListCell.h"

@interface CAFollowListCell()
@property (nonatomic,strong)UIImageView * headImgV;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UILabel * ageLabel;
@property (nonatomic,strong)UIButton * followBtn;
@property (nonatomic,strong)UIButton * blockBtn;

@end
@implementation CAFollowListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initView];
       
    }
    return  self;
}

- (void)initView{
    
    UIView * contentView = self.contentView;
    UIImageView * headImgv = [UIImageView new];
    headImgv.layer.cornerRadius = 22;
    headImgv.clipsToBounds = YES;
    self.headImgV = headImgv;
    [contentView addSubview:self.headImgV];
    [headImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.size.mas_equalTo(44);
        make.left.equalTo(contentView).offset(20);
        make.bottom.equalTo(contentView).offset(-16);
    }];
    
    UILabel * nameLabel = [UILabel LabelWithFont:Font(14) color:Hex(0x404040) text:@""];
    [contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImgv).offset(4);
        make.left.equalTo(headImgv.mas_right).offset(10);
        make.right.equalTo(contentView).offset(-90);
    }];
    
    UILabel * ageLabel = [UILabel LabelWithFont:Font(12) color:[UIColor colorWithWhite:0 alpha:0.3] text:@""];
    [contentView addSubview:ageLabel];
    [ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(8);
    }];
    self.ageLabel = ageLabel;
    
    
    UIButton * followBtn = [UIButton ButtonWithFont:Font(12) color:Hex(0x657C47) text:@"Unfollow"];
    self.followBtn = followBtn;
    [contentView addSubview:followBtn];
    
    [followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.right.equalTo(contentView).offset(-30);
    }];
    
    [followBtn addTarget:self action:@selector(unFollow) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton * blockBtn = [UIButton ButtonWithFont:Font(12) color:[UIColor whiteColor] text:@"Unblock"];
    blockBtn.layer.cornerRadius = 13;
    blockBtn.backgroundColor = Hex(0x454545);
    self.blockBtn = blockBtn;
    [contentView addSubview:blockBtn];
    
    [blockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(contentView);
        make.right.equalTo(contentView).offset(-30);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(26);
    }];
    
    [blockBtn addTarget:self action:@selector(unBlock) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setUser:(CAHomeUser *)user{
    _user = user;
    
    if (self.type == 1) {
        [self.headImgV sd_setImageWithURL:[NSURL URLWithString:user.avatarUrl] placeholderImage:Img(@"defult_head")];
        self.nameLabel.text = user.nickname;
        self.ageLabel.text = [NSString stringWithFormat:@"%@ %@",user.age,user.country];
        self.followBtn.hidden = NO;
        self.blockBtn.hidden = YES;
    }else{
        [self.headImgV sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:Img(@"defult_head")] ;
        self.nameLabel.text = user.nickName;
        self.ageLabel.text = [NSString stringWithFormat:@"%@ %@",user.age,user.registerCountry];
        self.followBtn.hidden = YES;
        self.blockBtn.hidden = NO;
    }
   
    
}


- (void)unFollow{
  
    [Http postWithURL:userUnFriend params:@{@"followUserId":self.user.userId} success:^(id  _Nonnull responseObject) {
            
        !self.removeBlock?:self.removeBlock();
        
        [CAUserInfo.shared.followIds removeObject:self.user.userId];
        [[NSNotificationCenter defaultCenter] postNotificationName:FollowDidChange object:nil];
        
        [EasyTextView showText:@"Unfollow Success"];
    } failure:^(id  _Nonnull errObject) {
        
    }];
    
}


- (void)unBlock{
    
    [Http postWithURL:removeBlock params:@{@"blockUserId":self.user.broadcasterId} success:^(id  _Nonnull responseObject) {
            
        !self.removeBlock?:self.removeBlock();
        
        [[NSNotificationCenter defaultCenter] postNotificationName:BlockDidChange object:nil];
      

       
        [EasyTextView showText:@"Unblock Success"];
        
        
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
}
@end
